<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Friends List</title>
</head>
<link rel="stylesheet" type="text/css" href="../css/master.css" media="screen" />
<body>
<script type="text/javascript">
	
String.prototype.startsWith = function(str) 
{return (this.match("^"+str)==str)}
	
	
function vote(cc)
{
	
	if(cc.startsWith("voteup"))
	{
		var id =  cc.substring(cc.indexOf(";") +1);
	
		
		document.getElementById("vote_type").value = "like";
		document.getElementById("votee_id").value = id;

		
		document.forms['gd'].submit();
		
		
	}
	else if(cc.startsWith("votedown"))
	{
		var id =  cc.substring(cc.indexOf(";") +1);
	
		
		document.getElementById("vote_type").value = "dislike";
		document.getElementById("votee_id").value = id;
		
		document.forms['gd'].submit();
		
		
		
	}
		
		
}


</script>

<fieldset>

<legend>Create a game</legend>

<div class="content creat_game">
<form name="gd" action="" method="POST">
	
	<table >
	
<?php include("../db.php");
session_start();
check_inactive();
authenticate_player(); 

$home_dir = "../";
$img_path = $home_dir."img/";




$db = pg_connect('host=dbsrv1 dbname=csc309g7 user=csc309g7 password=aiboid4p');

$myuserid = $_SESSION['userid'];

if($db)
{
			
	if($_POST['gameid'])
	{
		
		$gameid =$_POST['gameid'];
		
		
		
		if($_POST['votee_id'] && $_POST['vote_type'] )
		{
			
			$votee_id = $_POST['votee_id'];
			$vote_type = $_POST['vote_type'];
			
			
			add_vote($gameid, $myuserid, $votee_id, $vote_type);	
			
			
		}
	
	$getGames_query = "select gameid, sport_name, username, start_date, start_time, location_name, title from 
						(select * from (select * from games g left join location l on g.location = l.lid) a left join sports s on a.sid = s.sid) b 
						left join people p on b.creator = p.userid where gameid = ". $gameid;
	
	$getGames_result = pg_query($getGames_query);
	
	
	$getPlayerRating_query = "select ppl.userid userid,  ppl.username username, (ratings.likes + ratings.dislikes) total_votes, 
								to_char(ratings.likes::double precision/ (ratings.likes + ratings.dislikes) *100,'999.9') || '% ' rating from games 
								inner join sports on sports.sid = games.sid inner join location on location.lid = games.location 
								 inner join usergame ug on ug.gameid=games.gameid inner join people ppl on ppl.userid = ug.userid 
								 inner join ratings on ratings.userid = ug.userid where ug.gameid =$gameid and ratings.likes + ratings.dislikes <> 0 order by rating desc";
	
	
	//echo $getPlayerRating_query;
	
	$getPlayerRating_result = pg_query($getPlayerRating_query);
	

	
	if(pg_num_rows($getGames_result)!=0)
	{
		while($gamedetail = pg_fetch_assoc($getGames_result))
		{
?>

	<tr>
	<td>
		<label>Title: </label>	
	</td>
	<td>
		<label style="font-size:12px;"> <?= $gamedetail['title'] ?> </label>
	</td>
	</tr>
	<tr>
	<td>
		<label>Sports: </label>	
	</td>
	
	<td>
		<label style="font-size:12px;"> <?= $gamedetail['sport_name'] ?> </label>
	</td>
	</tr>
	<tr>
		
	<td>
		<label>Start Date: </label>
	</td>
	<td>	
		<label style="font-size:12px;"> <?= $gamedetail['start_date'] ?> </label>
	</td>
	</tr>
	
	
	<tr>
		<td>Start Time: </td>
		<td><label style="font-size:12px;"> <?= $gamedetail['start_time'] ?> </label></td>
	</tr>
	<tr>
		<td>Location: </td>
		<td>
			<label style="font-size:12px;"> <?= $gamedetail['location_name'] ?> </label>
		</td>
	</tr>
	<tr>
		<input type="hidden" id="vote_type" name="vote_type">
		<input type="hidden" id="votee_id" name="votee_id">
		<input type="hidden" id="showvote" name="showvote" value="showvote">
		<input type="hidden" id="gameid" name="gameid" value="<?=$gameid?>">
		
	</tr>
	
	
	
<?php 
			}

		}
		else //zero games found for gameid
		{
				echo "<tr><p>No game information found for your request</p></tr>";
		}
	}
	else // no gameid
	{
		
		echo "Error occur, please return to Game list page";
		
	}

	echo "</table>
	
	
	<table>
	<col width='50' />
	<col width='100' />
	<col width='100' />
	<col width='100' />
	<col width='200' />
	<col width='200' />
	";

	
	if(pg_num_rows($getPlayerRating_result)!=0)
	{
		$ind=0;
		while($results = pg_fetch_assoc($getPlayerRating_result))
		{
			
			$ind++;
?>
		  <tr height="50" >
		<td> <?=$ind ?> </td>  
		<td> <?= $results['username']  ?> </td>
		<td>  <?= $results['total_votes']  ?> votes</td>
		<td>  <?= $results['rating'] ?>  </td>		
		
		<?php 
		if($_POST['showvote']&& ($_POST['showvote'] == "showvote"))
		{
			
	
		$vote_up_exists = check_vote($gameid, $myuserid, $results['userid'], 0);
		
	
				
		if ($vote_up_exists)
		{
				echo "<td>  <a href=\"javascript:alert('You have already liked ".$results['username'] ."');\"><img src='".$img_path."liked.jpg' height='20' width='20' border='0' /> </a></td>";
		}
		else
		{
			echo "<td>  <a href=\"javascript:vote('voteup;".$results['userid']."');\"><img src='".$img_path."1.jpeg' height='20' width='20' border='0' /> </a></td>";
		}
		
		
		$vote_down_exists = check_vote($gameid, $myuserid, $results['userid'], 1);
		
		if ($vote_down_exists)
		{
				echo "<td>  <a href=\"javascript:alert('You have already disliked for ".$results['username'] ."');\"><img src='".$img_path."disliked.png' height='20' width='20' border='0' /> </a></td>";
		}
		else
		{
			echo "<td>  <a href=\"javascript:vote('votedown;".$results['userid']."');\"><img src='".$img_path."tdown.jpeg' height='20' width='20' border='0' /> </a></td>";
		}
		
		
	
		
		?>
		
		
		
		
		
		
		
		
		
		
		
		  </tr>
<?php



		}
		
		
		
	}
		
		
	} else {   //no players found
	      echo "<tr><p>No players have join this game yet</p></tr>";
	}
	
	
}


?>








</table>
</form>
</div>		

</fieldset>




</body>
</html>
